package Interceptor;

import static org.apache.struts2.StrutsStatics.HTTP_REQUEST;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import UserContainer.User;
import UserContainer.UserAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;



public class LoginInterceptor extends AbstractInterceptor {
	

	public String intercept(ActionInvocation invocation) throws Exception
	{
		ActionContext context = invocation.getInvocationContext();
		Map<String, User> session = context.getSession();
		
		User user = (User)session.get("user");
		
		if(user == null){
			HttpServletRequest request = (HttpServletRequest)context.get(HTTP_REQUEST);
			String queryString = request.getQueryString();
			String currentURL = request.getServletPath() + (queryString != null ?"?" + queryString : "");
			request.setAttribute("url", currentURL);
			
			return "sessionfail";
		}
		
		Object  action = invocation.getAction();
		
		if(action instanceof UserAware){
			((UserAware)action).setUser(user);
		}
		
		return invocation.invoke();
	}
}
